package com.finance.action.android;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.vo.Deduction;
import com.common.vo.JsonVo;
import com.finance.entity.Balance;
import com.finance.entity.BalanceRecord;
import com.finance.service.BalanceRecordService;
import com.finance.service.BalanceService;

@Controller("BalanceRecordAction")
public class BalanceRecordAction {

	@Resource(name = "BalanceService")
	private BalanceService balanceService;

	@Resource(name = "BalanceRecordService")
	private BalanceRecordService balanceRecordService;

	/**
	 * 没用！！！希望能用上
	 * 
	 * @param userId
	 *            业务员id
	 * @param amount
	 *            金额
	 * @param type
	 *            消费类型（1充值 2消费）
	 * @return
	 */
	@ResponseBody
	@RequestMapping("balance")
	public JsonVo<String> balance(String userId, Long amount, Integer type) {
		JsonVo<String> json = new JsonVo<String>();
		switch (type) {
		case 1:
			Balance balance = balanceService.selectByPrimaryKey(userId);
			Long count = balance.getBalanceCount() + amount;
			balance.setBalanceCount(count);
			balanceService.updateByPrimaryKeySelective(balance);

			BalanceRecord balanceRecord = new BalanceRecord();
			balanceRecord.setUserId(userId);
			balanceRecord.setRdTime(new Date());
			balanceRecord.setRdType(type);
			balanceRecord.setRdValue(amount);
			balanceRecord.setRdComment("充值");
			balanceRecordService.updateByPrimaryKeySelective(balanceRecord);
			break;
		case 2:
			Balance balance1 = balanceService.selectByPrimaryKey(userId);
			Long count1 = balance1.getBalanceCount() - amount;
			balance1.setBalanceCount(count1);
			balanceService.updateByPrimaryKeySelective(balance1);

			BalanceRecord balanceRecord1 = new BalanceRecord();
			balanceRecord1.setUserId(userId);
			balanceRecord1.setRdTime(new Date());
			balanceRecord1.setRdType(type);
			balanceRecord1.setRdValue(amount);
			balanceRecord1.setRdComment("消费");
			balanceRecordService.updateByPrimaryKeySelective(balanceRecord1);
			break;
		default:
			break;
		}
		return json;
	}

	@ResponseBody
	@RequestMapping("findbalance")
	public JsonVo<Balance> findbalance(String userId) {
		JsonVo<Balance> json = new JsonVo<Balance>();
		Balance balance = balanceService.selectByPrimaryKey(userId);
		if (balance == null) {
			json.setCode(1);
			json.setSuccess(false);
			json.setMsg("暂时没有数据，错误");
			return json;
		}
		json.setCode(200);
		json.setMsg("拉取成功");
		json.setSuccess(true);
		json.setContent(balance);
		return json;
	}

	@ResponseBody
	@RequestMapping("findBalanceRecord")
	public JsonVo<List<BalanceRecord>> findbalancerecord(String userId) {
		JsonVo<List<BalanceRecord>> json = new JsonVo<List<BalanceRecord>>();
		List<BalanceRecord> list = balanceRecordService.findByUserId(userId);
		if (list.size() == 0) {
			json.setCode(1);
			json.setSuccess(false);
			json.setMsg("暂时没有数据，错误");
			return json;
		}
		json.setCode(200);
		json.setMsg("拉取成功");
		json.setSuccess(true);
		json.setContent(list);
		return json;

	}
	/**
	 * 查询业务员账号余额
	 * @param userId 业务员id
	 * @param type 类型（1淘宝 2京东 3运营商 4定位）
	 * @return
	 */
	@ResponseBody
	@RequestMapping("userBalance")
	public JsonVo<String> userBalance(String userId, Integer type) {
		JsonVo<String> json = new JsonVo<String>();
		switch (type) {
		case 1:
			Balance balance = balanceService.selectByPrimaryKey(userId);
			Long taobao =balance.getBalanceCount() - Deduction.Taobao;
			if (taobao < 0 ) {
				json.setCode(1);
				json.setSuccess(false);
				json.setMsg("账号余额不足");
				return json;
			}
			json.setCode(200);
			json.setSuccess(true);
			json.setMsg("余额很充足，可以进行授权");
			break;
		case 2:
			Balance balance1 = balanceService.selectByPrimaryKey(userId);
			Long jingdong =balance1.getBalanceCount() - Deduction.Jingdong;
			if (jingdong < 0 ) {
				json.setCode(1);
				json.setSuccess(false);
				json.setMsg("账号余额不足");
				return json;
			}
			json.setCode(200);
			json.setSuccess(true);
			json.setMsg("余额很充足，可以进行授权");
			break;
		case 3:
			Balance balance2 = balanceService.selectByPrimaryKey(userId);
			Long yunying =balance2.getBalanceCount() - Deduction.YunYing;
			if (yunying < 0 ) {
				json.setCode(1);
				json.setSuccess(false);
				json.setMsg("账号余额不足");
				return json;
			}
			json.setCode(200);
			json.setSuccess(true);
			json.setMsg("余额很充足，可以进行授权");
			break;
		case 4:
			Balance balance3 = balanceService.selectByPrimaryKey(userId);
			Long dingwei =balance3.getBalanceCount() - Deduction.Location;
			if (dingwei < 0 ) {
				json.setCode(1);
				json.setSuccess(false);
				json.setMsg("账号余额不足");
				return json;
			}
			json.setCode(200);
			json.setSuccess(true);
			json.setMsg("余额很充足，可以进行授权");
			break;

		default:
			json.setCode(1);
			json.setSuccess(false);
			json.setMsg("类型有误");
			break;
		}
		return json;
	}
}
